package controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import model.Race;
import model.User;
import service.RaceService;
import service.impl.RaceServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

/**
 * @Author: 燕子豪
 * @Date: 2022/12/17 15:40
 */
@WebServlet("/raceListFindAll")
public class RaceListFindAllServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        RaceService raceService = new RaceServiceImpl();
        //将前端的参数转为json
        BufferedReader reader = new BufferedReader(request.getReader());
        StringBuilder json = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
            json.append(line);
        }
        JSONObject jsonObject = JSON.parseObject(json.toString());

        //解析前端的参数：pageIndex，pageSize暂指定为5
        int pageIndex = Integer.parseInt(jsonObject.getString("pageIndex"));
        List<Race> races = raceService.getAllRaces(5,pageIndex);

        //将数据List打包为json发给前端
        JSONObject message = new JSONObject();
        message.put("racesMsg",races);
        response.getWriter().write(message.toString());
    }
}
